package com.qk.ezbuy.myinterceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Myinterceptor implements HandlerInterceptor {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Cookie[] cookie = request.getCookies();
        String str =null;
        for(int i = 0;i<cookie.length;i++){
            if(cookie[i].getName().equals("login")){
                str= cookie[i].getValue();
            }
        }
        if(redisTemplate.hasKey(str)){
            return true;
        }else {
            response.sendRedirect("/index.html");
            return false;
        }

    }
}
